package com.zhongzhi.javafx.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Timer;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.GrabDetail;
import com.zhongzhi.common.Plc;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.StationShow;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;

import javafx.scene.control.Tab;

public class TABController implements Initializable{
	@FXML
	private Tab TAB;
	@FXML
	private ScrollPane SP;
	@FXML
	private FlowPane FP;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		TAB.setOnSelectionChanged(new EventHandler<Event>() {

			@Override
			public void handle(Event event) {

				Tab tab = (Tab)event.getSource();
				Plc plc = (Plc)tab.getUserData();
				Common.setPlc(plc);
				
				if(FP.getChildren().size()==0) {
					try {
						

						FP.getChildren().clear();
						tab = (Tab)event.getSource();
						plc = (Plc)tab.getUserData();
						Common.setPlc(plc);
						Common.setGrup(new Integer(0));
						
						HashMap<Integer, StationShow> stationMap = plc.getStTemData();
						Set<Integer> keySet = stationMap.keySet();
						for (Integer integer : keySet) {
							StationShow sShow = stationMap.get(integer);
							plc.getMap().put(integer, new GrabDetail(sShow, plc.getId()) );
						}


						AnchorPane pane = FXMLLoader.load(getClass().getResource("/fxml/mod/PLC.fxml"));

						FP.getChildren().add(pane);
						HashMap<Integer, GrabDetail> map = plc.getMap();
						Set<Integer> keySet2 = map.keySet();
						Iterator<Integer> it = keySet2.iterator();
						while(it.hasNext()) {
							Integer next = it.next();
							GrabDetail grabDetail = map.get(next);
							AnchorPane grab = FXMLLoader.load(getClass().getResource("/fxml/mod/GRAD.fxml"));
							grab.setUserData(grabDetail);
							FP.getChildren().add(grab);
							
						}


					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});

	}

}
